package artibella.media.controls 
{
	import artibella.IDisposable;
	import artibella.media.AMedia;
	import artibella.media.APlayer;
	import artibella.loaders.ILoaderAgent;
	import artibella.text.CrawlingLabel;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class AController extends Sprite implements ILoaderAgent, IDisposable
	{
		protected var _player:APlayer;
		protected var _media:AMedia;
		
		protected var _bt_play:MovieClip;
		protected var _bt_stop:MovieClip;
		protected var _prog_tool:MovieClip;
		protected var _time_tool:MovieClip;
		protected var _volume_tool:MovieClip;
		
		protected var _bt_prev:MovieClip;
		protected var _bt_next:MovieClip;
		protected var _bt_info:MovieClip;
		protected var _bt_list:MovieClip;
		
		protected var _bt_fullscreen:MovieClip;
		
		protected var _label:CrawlingLabel;
		
		protected var _paused:Boolean;
				
		
		public function AController() 
		{
			_paused = false;
		}
		
		// - - -
		
		public function init():void 
		{
			// override this
		}
		
		// - - -
		
		public function set player( p:APlayer ):void
		{
			_player = p;
		}
		
		public function set media( t:AMedia ):void
		{
			if ( _media != null ) {
				_media.dispose();
				_media = null;
			}
			
			_media = t;
			
			//SoundManager.instance.addSound( this, t ); 
		}
		
		///////  C O N T R O L S
		
		public function setPlayBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_play = mc;
			_bt_play.x = px;
			_bt_play.y = py;
			_bt_play.addEventListener( MouseEvent.CLICK, _doPause );
			
			addChild( _bt_play );
		}
		
		public function setStopBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_stop = mc;
			_bt_stop.x = px ;
			_bt_stop.y = py;
			_bt_stop.addEventListener( MouseEvent.CLICK, _doStop );
			
			addChild( _bt_stop );
		}
		
		public function setProgressionTool( mc:MovieClip, px:int, py:int ):void
		{
			_prog_tool = mc;
			_prog_tool.x = px;
			_prog_tool.y = py;
			
			addChild( _prog_tool );
		}
		
		public function setTimeTool( mc:MovieClip, px:int, py:int ):void
		{
			_time_tool = mc;
			_time_tool.x = px;
			_time_tool.y = py;
			
			addChild( _time_tool );
		}
		
		public function get time_tool():MovieClip
		{
			return _time_tool;
		}
		
		
		public function setVolumeTool( mc:MovieClip, px:int, py:int ):void
		{
			_volume_tool = mc;
			_volume_tool.x = px;
			_volume_tool.y = py;
			
			addChild( _volume_tool );
		}
		
		public function setFullScreenBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_fullscreen = mc;
			_bt_fullscreen.x = px ;
			_bt_fullscreen.y = py;
			_bt_fullscreen.addEventListener( MouseEvent.CLICK, _doFullScreen );
			
			addChild( _bt_fullscreen );
		}
		
		////// 
		
		public function setVolume( n:Number ):void
		{
			_player.defaultVolume = n;

			_media.volume = n;
		}
		
		public function setPlayState():void
		{
			_doPlay( null );
		}
		
		public function setPauseState():void
		{
			_doPause( null );
		}
		
		public function setSeek( p:Number ):void
		{
			var position:int = p * _media.duration; // audio millisec, video sec
			
			_media.seek( position, _paused );
		}
		
		public function setTime( p:Number ):void
		{
			if ( _time_tool != null ) _time_tool.timeUpdate( p * _media.duration );
		}

		// - - -
		
		public function setPrevBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_prev = mc;
			_bt_prev.x = px;
			_bt_prev.y = py;
			_bt_prev.addEventListener( MouseEvent.CLICK, _playPrev );
			
			addChild( _bt_prev );
		}
		
		public function setNextBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_next = mc;
			_bt_next.x = px;
			_bt_next.y = py;
			_bt_next.addEventListener( MouseEvent.CLICK, _playNext );
			
			addChild( _bt_next );
		}
		
		public function setInfoBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_info = mc;
			_bt_info.x = px;
			_bt_info.y = py;
			_bt_info.addEventListener( MouseEvent.CLICK, _showInfos );
			
			addChild( _bt_info );
		}
		
		public function setListBt( mc:MovieClip, px:int, py:int ):void
		{
			_bt_list = mc;
			_bt_list.x = px;
			_bt_list.y = py;
			_bt_list.addEventListener( MouseEvent.CLICK, _showList );
			
			addChild( _bt_list );
		}
		
		// - - -
		
		public function setLabel( mc:CrawlingLabel, px:int, py:int ):void
		{
			_label = mc;
			_label.x = px;
			_label.y = py;
			
			addChild( _label );
		}
		
		public function set labelText( s:String ):void
		{
			if ( _label != null ) _label.text = s;
		}
		
		/////// A C T I O N S
		
		protected function _doPlay( e:MouseEvent ):void
		{
			//trace("_doPlay" );
			
			_paused = false;
			
			_media.play();
		}
		
		protected function _doPause( e:MouseEvent ):void
		{
			//trace("_doPause" );
			
			if ( e != null ) _paused = true;
			
			_media.pause();
		}
		
		protected function _doStop( e:MouseEvent ):void
		{
			_paused = true;
			
			_media.stop();
		}
		
		// - - -
		
		protected function _playPrev( e:MouseEvent ):void
		{
			if ( _time_tool != null ) _time_tool.reset();
			if ( _prog_tool != null ) _prog_tool.progUpdate( 0 );
			
			_player.previous();
		}

		protected function _playNext( e:MouseEvent ):void
		{
			if ( _time_tool != null ) _time_tool.reset();
			if ( _prog_tool != null ) _prog_tool.progUpdate( 0 );
			
			_player.next();
		}
		
		protected function _showInfos( e:MouseEvent ):void
		{
			//trace( "_showInfos" );
		}
		
		protected function _hideInfos( e:MouseEvent ):void
		{
			//trace( "_showInfos" );
		}
		
		protected function _showList( e:MouseEvent ):void
		{
			//trace( _player.list.items );
		}
		
		protected function _hideList( e:MouseEvent ):void
		{
			//trace( "_showInfos" );
		}
		
		protected function _doFullScreen( e:MouseEvent ):void
		{
			if ( stage != null )
			{
				if ( stage.displayState == StageDisplayState.NORMAL )
				{
					stage.displayState = StageDisplayState.FULL_SCREEN;
				}
				else 
				{	
					stage.displayState = StageDisplayState.NORMAL;
				}
			}
		}
		
		// - - - ILoaderAgent
		
		public function addIndicator():void {}
		
		public function removeIndicator():void {}
		
		public function updateIndicator( niveau:int ):void
		{
			if ( _prog_tool != null ) _prog_tool.loadUpdate( niveau / 100 );
		}
		
		// - - - IStageObserver
		
		public function stageUpdate( w:int, h:int ):void
		{
			
		}
		
		// - - - IDisposable
		
		public function dispose():void
		{
			if ( _media != null ) {
				_media.dispose();
				_media = null;
			}
			
			if ( _prog_tool != null ) {
				_prog_tool.dispose();
				_prog_tool = null;
			}
			
			if ( _volume_tool != null ) {
				_volume_tool.dispose();
				_volume_tool = null;
			}
			
			_player = null;
		}
	}
}